package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.bean.Course;
import com.example.service.CourseService;
import com.example.service.StudentCourseService;
import com.example.service.UserService;
import com.example.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @title: Course
 * @Author wjf
 * @Date: 2022/9/29 14:34
 * @Version 1.0
 */
@RestController
@CrossOrigin
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private StudentCourseService studentCourseService;

    @GetMapping("/page")
    @CrossOrigin
    public Result<IPage<Course>> getAllUser(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer pageSize,
            @RequestParam String name) {
        IPage<Course> page = new Page<>(current, pageSize);
        IPage<Course> page1 = courseService.findPage(page,name);

//        //根据course表的教师id去查user表，设置教师的昵称
//        List<Course> records = page1.getRecords();
//         for (Course course : records){
//             if (course.getTeacherId()!=null){
//                 User user = userService.getById(course.getTeacherId());
//                 course.setTeacherName(user.getNickname());
//             }
//         }

        return Result.ok(page1);
    }

    @GetMapping({"/byName/{name}"})
    @CrossOrigin(allowCredentials = "true")
    public Result getUserByName(@PathVariable String name){
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(name),"name",name);
        Course one = courseService.getOne(queryWrapper);
        return Result.ok(one);
    }

    @GetMapping("/{id}")
    @CrossOrigin
    public Result<Course> getCourseById(@PathVariable("id") Integer id){
        Course course = courseService.getById(id);
        return Result.ok(course);
    }

    @GetMapping("/byTeacher/{id}")
    public Result getCourses(@PathVariable Integer id){
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",id);
        List<Course> list = courseService.list(queryWrapper);
        return Result.ok(list);
    }

    @PostMapping("/save")
    @CrossOrigin
    public Result<Boolean> saveUser(@RequestBody() Course course) {
        boolean save = courseService.save(course);
        return Result.ok(save);
    }

    @PutMapping("/update")
    public Result<Boolean> putUser(@RequestBody Course course) {
        boolean b = courseService.updateById(course);
        return Result.ok(b);
    }

    @DeleteMapping("/delete/{id}")
    @CrossOrigin
    public Result<Boolean> deleteUser(@PathVariable Integer id) {
        boolean b = courseService.removeById(id);
        return Result.ok(b);
    }

    @DeleteMapping("/delete/batch/{ids}")
    public Result<Boolean> deleteMoreUser(@PathVariable List<Integer> ids){
        boolean b = courseService.removeByIds(ids);
        return Result.ok(b);
    }

    /*
    选课
     */
    @PostMapping("/save/{courseId}/{studentId}")
    public Result setCourseAndStudent(@PathVariable("courseId") Integer courseId,@PathVariable("studentId") Integer studentId){
          Boolean b = courseService.setCourseAndStudent(courseId,studentId);
        if (b){
           return Result.ok().message("选课成功");
        }else {
            return Result.fail().message("该课程已选");
        }
    }

    /**
     * 多选课程接口
     * @param ids
     * @param studentId
     * @return
     */
    @PostMapping("/save/batch/{studentId}/{ids}")
    public Result selectMore(@PathVariable("ids") List<Integer> ids,@PathVariable("studentId") Integer studentId ){
        Boolean b=  courseService.selectMore(ids,studentId);
        if (b){
            return Result.ok().message("选课成功");
        }
        return Result.ok().message("选课失败");
    }

    @GetMapping("/courses/{studentId}")
    public Result searchCourses(@PathVariable Integer studentId){
       List<Course> courses =  studentCourseService.searchCourses(studentId);
      courses.forEach(m->{

      });
        System.out.println(courses);
       return Result.ok(courses);
    }
}
